package com.ruoyi.system.service.impl;
import com.alibaba.fastjson2.JSON;
import com.google.common.collect.Lists;

import java.util.*;

import com.ruoyi.common.enums.YnStatusEnum;
import com.ruoyi.system.domain.PermitReportFile;
import com.ruoyi.system.domain.ViewRelationMetadata;
import com.ruoyi.system.mapper.ViewRelationMetadataMapper;
import com.ruoyi.system.service.PermitReportFileService;
import com.ruoyi.system.service.ViewRelationMetadataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

/**
 * (ViewRelationMetadata)表服务实现类
 *
 * @author makejava
 * @since 2022-12-16 20:24:00
 */
@Slf4j
@Service("viewRelationMetadataService")
public class ViewRelationMetadataServiceImpl implements ViewRelationMetadataService {
    @Resource
    private PermitReportFileService permitReportFileService;
    @Resource
    private ViewRelationMetadataMapper viewRelationMetadataMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public ViewRelationMetadata queryById(Integer id) {
        return this.viewRelationMetadataMapper.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param viewRelationMetadata 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<ViewRelationMetadata> queryByPage(ViewRelationMetadata viewRelationMetadata, PageRequest pageRequest) {
        long total = this.viewRelationMetadataMapper.count(viewRelationMetadata);
        return new PageImpl<>(this.viewRelationMetadataMapper.queryAllByLimit(viewRelationMetadata, pageRequest), pageRequest, total);
    }

    /**
     * 查询指定行数据
     *
     * @param viewRelationMetadata 查询条件
     * @return 对象列表
     */
    @Override
    public List<ViewRelationMetadata> queryAll(ViewRelationMetadata viewRelationMetadata) {
        return viewRelationMetadataMapper.queryAll(viewRelationMetadata);
    }

    /**
     * 新增数据
     *
     * @param viewRelationMetadata 实例对象
     * @return 实例对象
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ViewRelationMetadata insert(ViewRelationMetadata viewRelationMetadata) {
        log.info("新增数据 参数:{}",JSON.toJSON(viewRelationMetadata));
        //获取元数据Id
        String[] split = null;
        if (viewRelationMetadata.getMetadataId().contains(",")) {
            split = viewRelationMetadata.getMetadataId().split(",");
        } else {
            split = new String[]{viewRelationMetadata.getMetadataId()};
        }
        List<String> metadataIds = Arrays.asList(split);
        if (!CollectionUtils.isEmpty(metadataIds)) {
            //根据元数据id 这么写的原因在于保留之前记录
            viewRelationMetadata.setMetadataIds(metadataIds);
            List<ViewRelationMetadata> viewRelationMetadataList =
                    viewRelationMetadataMapper.queryAllByConfViewNameAndMetadataIds(viewRelationMetadata);
            log.info("根据元数据id 查询是否存在 结果:{}",JSON.toJSON(viewRelationMetadataList));
            if (!CollectionUtils.isEmpty(viewRelationMetadataList)) {
                viewRelationMetadata.setYn(YnStatusEnum.N.getCode());
                //将之前数据更新
                viewRelationMetadataMapper.updateByConfViewNameAndMetadataIds(viewRelationMetadata);
            }

            PermitReportFile permitReportFile = permitReportFileService.queryById(viewRelationMetadata.getFileId());
            if (null != permitReportFile) {
                //先取证件,证件取不到,取报告
                if (null == permitReportFile.getPermitFileId()) {
                    viewRelationMetadata.setMetadataFileName(permitReportFile.getPermitName());
                    viewRelationMetadata.setFileId(permitReportFile.getPermitFileId());
                    viewRelationMetadata.setFileName(permitReportFile.getReportFileName());
                    viewRelationMetadata.setFileUrl(permitReportFile.getPermitFlieUrl());
                } else {
                    viewRelationMetadata.setMetadataFileName(permitReportFile.getReportName());
                    viewRelationMetadata.setFileId(permitReportFile.getReportFileId());
                    viewRelationMetadata.setFileName(permitReportFile.getReportFileName());
                    viewRelationMetadata.setFileUrl(permitReportFile.getReportFileUrl());
                }
            }

            viewRelationMetadata.setYn(YnStatusEnum.Y.getCode());
            this.viewRelationMetadataMapper.insert(viewRelationMetadata);
        }

        return viewRelationMetadata;
    }

    /**
     * 修改数据
     *
     * @param viewRelationMetadata 实例对象
     * @return 实例对象
     */
    @Override
    public ViewRelationMetadata update(ViewRelationMetadata viewRelationMetadata) {
        this.viewRelationMetadataMapper.update(viewRelationMetadata);
        return this.queryById(viewRelationMetadata.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.viewRelationMetadataMapper.deleteById(id) > 0;
    }

    @Override
    public Map<String, ViewRelationMetadata> queryMappingRelation(String configViewName, List<Integer> ids) {
        Map<String, ViewRelationMetadata> relationMetadataMap = new ConcurrentHashMap<>();

        ViewRelationMetadata viewRelationMetadata = new ViewRelationMetadata();
        viewRelationMetadata.setConfViewName(configViewName);
        viewRelationMetadata.setMetadataIds(ids.stream().map(String::valueOf).collect(Collectors.toList()));
        List<ViewRelationMetadata> viewRelationMetadataList = viewRelationMetadataMapper.queryAllByConfViewNameAndMetadataIds(viewRelationMetadata);

        if (CollectionUtils.isEmpty(viewRelationMetadataList)) {
            return relationMetadataMap;
        }

        return viewRelationMetadataList.stream().collect(Collectors.toMap(key -> key.getConfViewName() + key.getMetadataId(), el -> el, (e1, e2) -> e1));
    }

}
